package com.zzz.bookdemo.controller;

/*
 * @author zzr
 * @date: 2025/09/16  19:43
 * @description:
 */

import com.zzz.bookdemo.constants.Constants;
import com.zzz.bookdemo.model.*;
import com.zzz.bookdemo.service.BookService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Slf4j
@RestController
@RequestMapping("/book")
public class BookController {

    @Autowired
    private BookService bookService;


    @RequestMapping("/addBook")
    public String addBook(BookInfo bookInfo) {
        log.info("添加图书：{}", bookInfo); // 日志打印 便于我们观察代码~

        if (!StringUtils.hasLength(bookInfo.getBookName())
        || !StringUtils.hasLength(bookInfo.getAuthor())
        || null == bookInfo.getCount()
        || null == bookInfo.getPrice()
        || !StringUtils.hasLength(bookInfo.getPublish())
        || null == bookInfo.getStatus()) {
            return "参数输入不合法，请检查参数";
        }

        try{
            bookService.addBook(bookInfo);
            return "";
        } catch (Exception e) {
            log.error("添加图书失败", e);
            return e.getMessage();
        }
    }

    @RequestMapping("/getListByPage")
    public Result getListByPage(PageRequest pageRequest, HttpSession session) {
        log.info("获取图书列表，pageRequest：{}",pageRequest);

//        // 判断用户是否登录
//        if (session.getAttribute(Constants.SESSION_USER_KEY) == null) {
//            return Result.unlogin();
//        }
//
//        UserInfo userInfo = (UserInfo) session.getAttribute(Constants.SESSION_USER_KEY);
//        if (userInfo == null || userInfo.getId() < 0 || "".equals(userInfo.getUserName())) {
//            return Result.unlogin();
//        }

        // 用户登录，返回图书列表
        PageResult<BookInfo> pageResult = bookService.getBookListByPage(pageRequest);
        return Result.success(pageResult);
    }

    @RequestMapping("/queryBookById")
    public BookInfo queryBookById(Integer bookId) {
        if (null == bookId || bookId < 0) {
            return new BookInfo();
        }
        log.info("传入id：" + bookId);
        BookInfo bookInfo = bookService.queryBookById(bookId);
        return bookInfo;
    }

    @RequestMapping("/updateBook")
    public String updateBook(BookInfo bookInfo) {
        log.info("修改图书：{}", bookInfo);
        try{
            bookService.updateBook(bookInfo);
            return "";
        } catch (Exception e) {
            log.error("修改图书失败", e);
            return e.getMessage();
        }
    }

    @RequestMapping(value = "/deleteBook",produces = "application/json")
    public String deleteBook(Integer bookId) {
        log.info("删除图书,bookId:{}",bookId);

        try{
            Integer result = bookService.deleteBookById(bookId);

            if (result > 0) {
                return "";
            }
            return "出现错误";
        } catch (Exception e) {
            log.error("更新失败,e:",e);
            return "内部错误";
        }
    }

    @RequestMapping("/batchDeleteBook")
    public boolean batchDeleteBook(@RequestParam List<Integer> ids) {
        log.info("批量删除图书，ids：{}",ids);
        try {
            bookService.batchDeleteBook(ids);
        } catch (Exception e) {
            log.error("批量删除发生异常，e：{}",e);
            return false;
        }
        return true;
    }
}
